package com.tao.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;
import java.util.Set;

/**
 * @ClassName MyInterceptor
 * @Author HETAO
 * @Date 2020/8/2 21:08
 */
@Intercepts({
        @Signature(
                type = Executor.class,
                method="query",
                args={MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
})
public class MyInterceptor implements Interceptor {
    private Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement statement = (MappedStatement)args[0];
        Integer timeout = statement.getTimeout();
        System.out.println("timeout = " + timeout);
        Object result = invocation.proceed();
        System.out.println("\"自定义插件\" = " + "自定义插件");
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
